package com.project.service;

import com.project.dao.CvMapper;
import com.project.entity.Cv;
import com.project.service.base.BaseService;
import com.project.utils.FileUtil;
import com.project.utils.Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//让checked例外也回滚
@Transactional(rollbackFor = Exception.class)
@Service
public class CvServiceImpl extends BaseService implements CvService {

    private static CvMapper cvMapper = null;

    public void setMapper() {
        if (cvMapper == null)
            cvMapper = sqlSessionTemplate.getMapper(CvMapper.class);//1.获取相关mapper
    }

    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 添加一个简历
     *
     * @param cv
     * @return
     */
    @Override
    public int addOne(Cv cv) {
        setMapper();
        return cvMapper.insertSelective(cv);
    }

    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 删除一个简历
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeOneById(Integer id,String URL) {
        setMapper();
        boolean flag = false;
        if (cvMapper.deleteByPrimaryKey(id) > 0 && FileUtil.delFile(URL)) flag = true;
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/9/4
     * function: 修改一个简历
     *
     * @param cv
     * @return
     */
    @Override
    public int modifyOneById(Cv cv) {
        setMapper();
        return cvMapper.updateByPrimaryKeySelective(cv);
    }

    /**
     * author: WheelChen
     * date: 2017/9/5
     * function:
     *
     * @param id
     * @return
     */
    @Override
    public Cv findOneById(Integer id) {
        setMapper();
        return cvMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/9/7
     * function: 根据条件模糊查找简历
     *
     * @param cv
     * @return
     */
    @Override
    public List<Cv> findByCondition(Cv cv) {
        setMapper();
        return cvMapper.selectByCondition(cv);
    }
    /**
     * @param stid
     * @return
     */
    @Override
    public List<Cv> findBatchByStid(Integer stid) {
        setMapper();
        return cvMapper.selectBatchByStid(stid);
    }
}
